/* Copyright aQute SARL, All Rights Reserved
 * All rights reserved. This program and the accompanying materials
 * are made available under the terms of the Eclipse Public License v1.0
 * which accompanies this distribution, and is available at
 * http://www.eclipse.org/legal/epl-v10.html
 * 
 */
package org.nanhill.osgi.fileinstall.impl;

import java.util.Dictionary;
import java.util.HashMap;
import java.util.Hashtable;
import java.util.Iterator;
import java.util.Map;

import org.osgi.framework.BundleActivator;
import org.osgi.framework.BundleContext;
import org.osgi.framework.Constants;
import org.osgi.service.cm.ConfigurationAdmin;
import org.osgi.service.cm.ConfigurationException;
import org.osgi.service.cm.ManagedServiceFactory;
import org.osgi.service.packageadmin.PackageAdmin;
import org.osgi.util.tracker.ServiceTracker;

/**
 * This clever little bundle watches a directory and will install any jar file
 * if finds in that directory (as long as it is a valid bundle and not a
 * fragment).
 * 
 */

public class Activator implements BundleActivator, ManagedServiceFactory {
	static ServiceTracker padmin;
	static ServiceTracker cmTracker;

	private BundleContext context;
	private Map<String, Object> watchers = new HashMap<String, Object>();

	public void start(BundleContext context) throws Exception {
		System.out.println("FILEINSTALL | nanhill (c) 2007 v1.3");
		this.context = context;
		Hashtable<String, String> props = new Hashtable<String, String>();
		props.put(Constants.SERVICE_PID, getName());
		context.registerService(ManagedServiceFactory.class.getName(), this,
				props);

		padmin = new ServiceTracker(context, PackageAdmin.class.getName(), null);
		padmin.open();
		cmTracker = new ServiceTracker(context, ConfigurationAdmin.class
				.getName(), null);
		cmTracker.open();

		// Created the initial configuration
		Hashtable<String, String> ht = new Hashtable<String, String>();
		set(ht, DirectoryWatcher.POLL);
		set(ht, DirectoryWatcher.DIR);
		set(ht, DirectoryWatcher.DEBUG);
		updated("initial", ht);
	}

	private void set(Hashtable<String, String> ht, String key) {
		String o = (String) context.getProperty(key);
		if (o == null)
			return;
		ht.put(key, o);
	}

	public void stop(BundleContext context) throws Exception {
		for (Iterator<Object> itt = watchers.values().iterator(); itt.hasNext();) {
			Object item = itt.next();
			if (item instanceof DirectoryWatcher) {
				DirectoryWatcher dir = (DirectoryWatcher) item;
				itt.remove();
				dir.close();
			}
		}
		cmTracker.close();
		padmin.close();
	}

	public void deleted(String pid) {
		DirectoryWatcher watcher = (DirectoryWatcher) watchers.remove(pid);
		if (watcher != null)
			watcher.close();
	}

	public String getName() {
		return this.getClass().getCanonicalName();
	}

	public void updated(String pid, Dictionary properties)
			throws ConfigurationException {
		deleted(pid);
		DirectoryWatcher watcher = new DirectoryWatcher(properties, context);
		watchers.put(pid, properties);
		watcher.start();
	}
}
